// 本表条件

// 生成一个随机的id
export const generateRandomId = () => {
  // 使用 Date.now() 获取当前时间戳
  const timestamp = Date.now()

  // 使用 Math.random() 生成一个随机数并转换为字符串
  const randomStr = Math.random().toString(36).substring(2, 8)

  // 将时间戳和随机数拼接起来作为ID
  const randomId = timestamp.toString() + randomStr

  return randomId
}

// 本表条件，根据选择控制下拉框的选项
const controlOptions = variableType => {
  let tableConditionOptionsTwo = []
  let tableConditionOptionsThree = []
  if (variableType === '10') {
    tableConditionOptionsTwo = [
      { label: '等于', value: '==' },
      { label: '不等于', value: '!=' },
      { label: '为空', value: '为空' },
      { label: '不为空', value: '不为空' },
    ]
    tableConditionOptionsThree = [
      { label: '与变量比较', value: '与变量比较' },
      { label: '与具体值比较', value: '与具体值比较' },
    ]
  } else if (
    variableType === '20' ||
    variableType === '40' ||
    variableType === '60' ||
    variableType === '70' ||
    variableType === '80' ||
    variableType === '100' ||
    variableType === '110' ||
    variableType === '120'
  ) {
    tableConditionOptionsTwo = [
      { label: '等于', value: '==' },
      { label: '不等于', value: '!=' },
      { label: '为空', value: '为空' },
      { label: '不为空', value: '不为空' },
      { label: '大于', value: '>' },
      { label: '小于', value: '<' },
      { label: '大于等于', value: '>=' },
      { label: '小于等于', value: '<=' },
    ]
    tableConditionOptionsThree = [
      { label: '与变量比较', value: '与变量比较' },
      { label: '与具体值比较', value: '与具体值比较' },
    ]
  } else if (variableType === '30' || variableType === '50' || variableType === '90') {
    tableConditionOptionsTwo = [
      { label: '等于', value: '==' },
      { label: '不等于', value: '!=' },
      { label: '为空', value: '为空' },
      { label: '不为空', value: '不为空' },
      { label: '大于', value: '>' },
      { label: '小于', value: '<' },
      { label: '大于等于', value: '>=' },
      { label: '小于等于', value: '<=' },
    ]
    tableConditionOptionsThree = [
      { label: '与变量比较', value: '与变量比较' },
      { label: '与具体值比较', value: '与具体值比较' },
      { label: '未来日期', value: '未来日期' },
    ]
  }
  return {
    tableConditionOptionsTwo,
    tableConditionOptionsThree,
  }
}

// 检查对象数组里面所有的属性都不为空
export const checkPropertiesNotEmpty = arr => {
  // 遍历数组
  for (let i = 0; i < arr.length; i++) {
    const obj = arr[i]
    // 遍历对象的属性
    for (const key in obj) {
      // 如果某个属性的值为空（undefined、null、空字符串等），则返回 false
      if (!obj[key]) {
        return false
      }
    }
  }
  // 如果所有属性都不为空，则返回 true
  return true
}

/* 
1. 点击增加条件
*/
export const btnTableConditionAddConPublic = (tableConditionSelectVal, tableConditionOptions, objArrValue) => {
  // 1. 根据选中的选项，控制下拉项
  const obj = tableConditionOptions.find(item => {
    return item.variable === tableConditionSelectVal
  })
  // 条件判断筛选两个下拉框的下拉选项
  const { tableConditionOptionsTwo, tableConditionOptionsThree } = controlOptions(obj.variableType)
  // 2. 数据驱动头部多层的渲染
  if (objArrValue.length === 0) {
    objArrValue.push({
      first: tableConditionSelectVal,
      second: '',
      third: '',
      fourth: '数值',
      firstSelect: '==',
      secondSelect: '与变量比较',
      id: generateRandomId(),
      tableConditionOptionsTwo: tableConditionOptionsTwo,
      tableConditionOptionsThree: tableConditionOptionsThree,
    })
  } else {
    objArrValue.push({
      first: tableConditionSelectVal,
      second: '',
      third: '',
      fourth: '数值',
      firstSelect: '==',
      secondSelect: '与变量比较',
      zero: '&&',
      id: generateRandomId(),
      tableConditionOptionsTwo: tableConditionOptionsTwo,
      tableConditionOptionsThree: tableConditionOptionsThree,
    })
  }
  return objArrValue
}

/* 
2. 点击确认
*/
export const btnTableConditionConfirmPublic = (
  objArrValue, // 数据驱动数据
  tableConTableName, // 表名surface
  tableConditionOptionsZeroEnums, // 并且或者下拉框enums
  tableConTableNameName, // 表名中文
  tableConditionOptionsEnums, // 原始下拉框enums
  tableConditionOptionsTwoEnums, // 等于小于下拉框enums
  tableConditionOptionsThree // 变量具体值下拉框enums
) => {
  // 2.1 一个参数
  let paramsString = ''
  // 2.2 二个参数
  let paramsStringTwo = ''
  objArrValue.forEach(item => {
    if (item.zero) {
      // 非首个
      if (item.firstSelect === '为空' || item.firstSelect === '不为空') {
        if (item.firstSelect === '为空') {
          paramsString =
            paramsString +
            ` ${item.zero} (${tableConTableName}.${item.first} == 'null' || ${tableConTableName}.${item.first}) == ''`

          paramsStringTwo =
            paramsStringTwo +
            ` ${tableConditionOptionsZeroEnums[item.zero]} ${tableConTableNameName}.${
              tableConditionOptionsEnums[item.first]
            } 为空`
        } else if (item.firstSelect === '不为空') {
          paramsString =
            paramsString +
            ` ${item.zero} (${tableConTableName}.${item.first} != 'null' || ${tableConTableName}.${item.first} != '')`

          paramsStringTwo =
            paramsStringTwo +
            ` ${tableConditionOptionsZeroEnums[item.zero]} ${tableConTableNameName}.${
              tableConditionOptionsEnums[item.first]
            } 不为空`
        }
      } else {
        if (item.secondSelect === '与变量比较') {
          paramsString =
            paramsString +
            ` ${item.zero} ${tableConTableName}.${item.first} ${item.firstSelect} ${tableConTableName}.${item.second}`

          paramsStringTwo =
            paramsStringTwo +
            ` ${tableConditionOptionsZeroEnums[item.zero]} ${tableConTableNameName}.${
              tableConditionOptionsEnums[item.first]
            } ${tableConditionOptionsTwoEnums[item.firstSelect]} ${tableConTableNameName}.${
              tableConditionOptionsEnums[item.second]
            }`
        } else if (item.secondSelect === '与具体值比较') {
          if (item.fourth === '数值') {
            paramsString =
              paramsString + ` ${item.zero} ${tableConTableName}.${item.first} ${item.firstSelect} ${item.third}`

            paramsStringTwo =
              paramsStringTwo +
              ` ${tableConditionOptionsZeroEnums[item.zero]} ${tableConTableNameName}.${
                tableConditionOptionsEnums[item.first]
              } ${tableConditionOptionsTwoEnums[item.firstSelect]} ${item.third}`
          } else if (item.fourth === '字符') {
            paramsString =
              paramsString + ` ${item.zero} ${tableConTableName}.${item.first} ${item.firstSelect} '${item.third}'`

            paramsStringTwo =
              paramsStringTwo +
              ` ${tableConditionOptionsZeroEnums[item.zero]} ${tableConTableNameName}.${
                tableConditionOptionsEnums[item.first]
              } ${tableConditionOptionsTwoEnums[item.firstSelect]} '${item.third}'`
          }
        } else if (item.secondSelect === '未来日期') {
          paramsString =
            paramsString + ` ${item.zero} ${tableConTableName}.${item.first} ${item.firstSelect} timeDate(\"0\")"`

          paramsStringTwo =
            paramsStringTwo +
            ` ${tableConditionOptionsZeroEnums[item.zero]} ${tableConTableNameName}.${
              tableConditionOptionsEnums[item.first]
            } ${tableConditionOptionsTwoEnums[item.firstSelect]} timeDate(\"0\")"`
        }
      }
    } else {
      // 首个
      if (item.firstSelect === '为空' || item.firstSelect === '不为空') {
        if (item.firstSelect === '为空') {
          paramsString =
            paramsString + `(${tableConTableName}.${item.first} == 'null' || ${tableConTableName}.${item.first}) == ''`

          paramsStringTwo = paramsStringTwo + `${tableConTableNameName}.${tableConditionOptionsEnums[item.first]} 为空`
        } else if (item.firstSelect === '不为空') {
          paramsString =
            paramsString + `(${tableConTableName}.${item.first} != 'null' || ${tableConTableName}.${item.first} != '')`

          paramsStringTwo =
            paramsStringTwo + `${tableConTableNameName}.${tableConditionOptionsEnums[item.first]} 不为空`
        }
      } else {
        if (item.secondSelect === '与变量比较') {
          paramsString =
            paramsString + `${tableConTableName}.${item.first} ${item.firstSelect} ${tableConTableName}.${item.second}`

          paramsStringTwo =
            paramsStringTwo +
            `${tableConTableNameName}.${tableConditionOptionsEnums[item.first]} ${
              tableConditionOptionsTwoEnums[item.firstSelect]
            } ${tableConTableNameName}.${tableConditionOptionsEnums[item.second]}`
        } else if (item.secondSelect === '与具体值比较') {
          if (item.fourth === '数值') {
            paramsString = paramsString + `${tableConTableName}.${item.first} ${item.firstSelect} ${item.third}`
            paramsStringTwo =
              paramsStringTwo +
              `${tableConTableNameName}.${tableConditionOptionsEnums[item.first]} ${
                tableConditionOptionsTwoEnums[item.firstSelect]
              } ${item.third}`
          } else if (item.fourth === '字符') {
            paramsString = paramsString + `${tableConTableName}.${item.first} ${item.firstSelect} '${item.third}'`

            paramsStringTwo =
              paramsStringTwo +
              `${tableConTableNameName}.${tableConditionOptionsEnums[item.first]} ${
                tableConditionOptionsTwoEnums[item.firstSelect]
              } '${item.third}'`
          }
        } else if (item.secondSelect === '未来日期') {
          paramsString = paramsString + `${tableConTableName}.${item.first} ${item.firstSelect} timeDate(\"0\")"`

          paramsStringTwo =
            paramsStringTwo +
            `${tableConTableNameName}.${tableConditionOptionsEnums[item.first]} ${
              tableConditionOptionsTwoEnums[item.firstSelect]
            } timeDate(\"0\")"`
        }
      }
    }
  })
  return {
    paramsString,
    paramsStringTwo,
  }
}
